package com.oss.Intercept;

import java.sql.Timestamp;
import java.util.Date;

// import org.joda.time.DateTime;
import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.utils.xx;
/*
import com.eova.config.EovaConst;
import com.eova.model.User;
import com.jfinal.core.Controller;
*/

public class pt_user extends MetaObjectIntercept {
	public String tableName = "pt_user";
	public String className = "com.oss.Intercept." + tableName;
	
	@Override
	 
	public void queryBefore(AopContext ac) throws Exception {
		// DIY 查询，名称有值，用名称里面的值来查状态列
		String user_id = InterceptUtil.getUserID(ac);
		String name = "";
		String method = "queryBefore";

		System.out.println(className + "." + method + "> " + "start ...");
		System.out.println(className + "." + method + "> " + "login_id=[" + user_id + "]");
		int i = 0;
		
		if ( (!user_id.equals("1")) && (!user_id.equals("2")) && (!user_id.equals("3")) && (!user_id.equals("4")) && (!user_id.equals("0")) ) {
			System.out.println(className + "." + method + "> " + "不是管理员,需要限制!");
			while (true) {
				name = ac.ctrl.getPara(i);
				System.out.println(className + "." + method + "> " + "name(" + String.valueOf(i) + ")=[" + name + "]");
				if ( name.indexOf("c_" + tableName) != -1 ) {
					ac.condition = " and id = '" + user_id + "'";
					 
					System.out.println(className + "." + method + "> " + "ac.condition=[" + ac.condition + "]");
					break;
				} else {
					if (xx.isEmpty(name)) {
						break;
					}
				}
		        i++;			
			}
			
		} else {
			System.out.println(className + "." + method + "> " + "是管理员,不需要限制!");
		}
			
		
		/*
		if (!xx.isEmpty(name)) {
			ac.condition = " state = " + name;
		}
		*/
		
		// 同理：想添加自定义查询条件，直接添加元字段
		// 然后配置为 可查询，禁止编辑，禁止新增，然后获取值
		// 利用本AOP方法可自定义查询sql即可
		// 应用场景：like or 子查询 范围查询 外表关联查询...
		System.out.println(className + "." + method + "> " + "end.");
	}

	public String getUserID(AopContext ac) throws Exception {
		String method = "getUserID";
		String user_id = "";
		int t;
		user_id = ac.user.toString();

		System.out.println(className + "." + method + "> " + "user_id=[" + user_id + "]");

		// user_id=[com.eova.model.User@ba45f869 {dtype:1047, dwid:421A085, email:null, id:2145218, insertowner:2145218, inserttime:2016-09-19 22:51:40.0, login_id:421A08501, login_pwd:1, mphone:null, nickname:咸宁市机关事务管理局用户, pstatus:1, rid:30011, updateowner:2145218, updatetime:2016-09-19 22:51:40.0, xb:1, xid:4212, zjbh:null, zjtype:1}]
		
		String SearchHeadString = ", id:"; 
		
		t = user_id.indexOf(SearchHeadString);
		
		if ( t != -1 ) {
			user_id = user_id.substring(t + SearchHeadString.length());
			t = user_id.indexOf(",");
			user_id = user_id.substring(0, t);
		} else {
			user_id = "0";
		}
		return user_id;	
		
	}
	
	public String getLoginID(AopContext ac) throws Exception {
		String login_id = ac.user.get("login_id");
		return login_id;
	}	
	
	public String updateBefore(AopContext ac) throws Exception {
		String method = "updateBefore";
		System.out.println(className + "." + method + "> start ...");
		String user_id = InterceptUtil.getUserID(ac);

		ac.record.set("updateowner", user_id);
		ac.record.set("updatetime", new Timestamp(new Date().getTime()));
		System.out.println(className + "." + method + "> end.");
		return super.updateBefore(ac);
	}

	public String addBefore(AopContext ac) throws Exception {
		String method = "addBefore";
		System.out.println(className + "." + method + "> start ...");
		String user_id = InterceptUtil.getUserID(ac);
		ac.record.set("insertowner", user_id);
		ac.record.set("inserttime", new Timestamp(new Date().getTime()));
		System.out.println(className + "." + method + "> end.");
		return super.addBefore(ac);
	}	
	
}

